package com.hkts.naturalgas.datahandler.sheldertask;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @ClassNameConnectionManagerTask
 * @Description 处理http过期链接
 * @Author yck
 * @Date2021-10-24 9:08
 * @Version V1.0
 **/
@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
@Lazy(false)
@Slf4j
public class ConnectionManagerTask {
    @Autowired
    private PoolingHttpClientConnectionManager connManager;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

  // @Scheduled(cron = "0/30 * * * * ?")
    public void CloseExpareConnection() {
        CompletableFuture<Void> voidCompletableFuture1 = CompletableFuture.runAsync(() -> {
            log.info("处理无效链接");
            connManager.getMaxTotal();
            connManager.closeExpiredConnections();
            connManager.closeIdleConnections(30, TimeUnit.SECONDS);
        }, threadPoolExecutor);
    }

}
